package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.dao.HousepriceMapper;
import com.wuming.house.model.Houseprice;
import com.wuming.house.model.HousepriceTrend;
import com.wuming.house.service.HousepriceService;
import com.wuming.house.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("housepriceService")
public class HousepriceServiceImpl implements HousepriceService {

    private static Logger logger = LoggerFactory.getLogger(HousepriceServiceImpl.class);

    @Autowired
    private HousepriceMapper housepriceMapper;

    @Override
    public void saveHouseprice(Houseprice houseprice) throws BusinessException {
        housepriceMapper.insert(houseprice);
    }

    @Override
    public void modifyHouseprice(Houseprice houseprice) throws BusinessException {
        housepriceMapper.updateByPrimaryKey(houseprice);
    }

    @Override
    public Houseprice getHousepriceById(Long id) throws BusinessException {
        return housepriceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Houseprice> getHousepriceByCity(String city, Integer year, Integer month) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("city", city);
        if (null != year) {
            params.put("year", year);
        }
        if (null != month) {
            params.put("month", month);
        }
        List<Houseprice> houseprices = housepriceMapper.selectByParams(params);
        return houseprices;
    }

    @Override
    public List<Houseprice> getHousepriceBetweenOneYear(String city) throws BusinessException {
        Calendar current = Calendar.getInstance();
        Integer endYear = current.get(Calendar.YEAR);
        Integer endMonth = current.get(Calendar.MONTH) + 1;
//        Date startDate = DateUtil.addMonth(-12);
//        GregorianCalendar startDateCal = new GregorianCalendar();
//        startDateCal.setTime(startDate);
//        Integer startYear = startDateCal.get(Calendar.YEAR);
//        Integer startMonth = startDateCal.get(Calendar.MONTH) + 1;
//        logger.info("开始年" + startYear + "开始月" + startMonth + "结束年" + endYear + "结束月" + endMonth);
        Map<String, Object> params = new HashMap<>();
        params.put("city", city);
//        params.put("startYear", startYear);
//        params.put("startMonth", startMonth);
        params.put("endYear", endYear);
        params.put("endMonth", endMonth);
        List<Houseprice> houseprices = housepriceMapper.selectByParams(params);
        List<Houseprice> oneYearHouseprices = new ArrayList<>();
        if (houseprices.size() >= 12) {
            oneYearHouseprices = houseprices.subList(0, 12);
        } else {
            oneYearHouseprices = houseprices.subList(0, houseprices.size());
        }
        logger.info("城市:{},一年之内的房价信息:{}", city, oneYearHouseprices);
        return oneYearHouseprices;
    }

    @Override
    public HousepriceTrend getTrendByHouseprices(List<Houseprice> houseprices) throws BusinessException {
        HousepriceTrend ht = new HousepriceTrend();
        List<String> months = new ArrayList<>();
        List<Integer> prices = new ArrayList<>();
        List<Integer> secondPrices = new ArrayList<>();
        for (Houseprice houseprice : houseprices) {
            months.add(houseprice.getYear() + "-" + houseprice.getMonth());
            prices.add(houseprice.getNewhousePrice());
            secondPrices.add(houseprice.getSecondhousePrice());
        }
        ht.setMonths(months);
        ht.setPrices(prices);
        ht.setSecondPrices(secondPrices);
        return ht;
    }

    @Override
    public void deleteById(Long id) {
        try {
            housepriceMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
